﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace FileTranslator
{
    public enum ColumnProperty
    {
        READONLY,
        PRIMARY_KEY,
        FOREIGN_KEY,
        NORMAL
    }

    public class DataContainer
    {
        #region Fields
        // 所属类型
        public string Category { get; set; }
        // 描述性列
        public string DescriptionColumn { get; set; }

        // GroupBy暗示用来在Load之后将大表根据此值进行分割
        public string[] GroupByHint { get; set; }

        // 用来存储数据View的部分
        private List<DataTableWrapper> dataCache = new List<DataTableWrapper>();

        // 真正的数据表
        public DataTable DataTable { get; set; }
        #endregion

        // 约束控制器，代表了包含于这个DataContainer中的所有的DataTableWrapper的公共约束
        public ConstraintHelper ConstraintHelper { get; set; }
        public DataContainer(ConstraintHelper vih)
        {
            this.ConstraintHelper = vih;
        }

        public void CreateWholeTableWrapper(DataView dt, List<DataTableWrapper> dtws)
        {
            List<DataTableWrapper> newlist = new List<DataTableWrapper>(dtws);
            DataTableWrapper dtw = new WholeTableViewWrapper(ConstraintHelper, newlist);
            dtw.InnerTable = dt;

            dataCache.Insert(0, dtw);
        }

        // dt中有InnerTable所需要的schema
        public void CreateNewDataTableWrapper(DataView dt)
        {
            DataTableWrapper dtw = new DataTableWrapper(ConstraintHelper);
            dtw.InnerTable = dt;

            if (GroupByHint != null)
            {
                // DataTableWrapper需要有自己的ConstraintHint
                dtw.ConstraintHelp = new ConstraintHelper();

                for (int i = 0; i < GroupByHint.Length; i++)
                {
                    string column = GroupByHint[i];
                    string value = dt[0][column].ToString();
                    dtw.AddConstraint(
                        new DefaultValueConstraint(column, value));
                }
            }

            dataCache.Add(dtw);
        }

        public DataTableWrapper GetDataTableWrapper(int index)
        {
            if (index < 0 || index >= dataCache.Count)
            {
                return null;
            }
            return dataCache[index];
        }

        public List<DataTableWrapper> GetDataTableWrappers()
        {
            return dataCache;
        }

        public void Clear()
        {
            foreach (DataTableWrapper dtw in dataCache)
            {
                dtw.Dispose();
            }
        }

        // 获得一份内部DataTable的副本
        DataTable Dump(int index)
        {
            return dataCache[index].DumpToDataTable();
        }

        // 获得一份内部DataTable的副本
        DataTable[] Dump()
        {
            DataTable[] dtArray = new DataTable[dataCache.Count()];
            for (int i = 0; i < dtArray.Count(); i++)
            {
                dtArray[i] = this.Dump(i);
            }
            return dtArray;
        }

        // 根据约束检查Column的属性，如果是外键，则返回相应的ForeignKeyConstraint
        public ColumnProperty CheckColumnProperty(string columnName, out FileTranslator.ForeignKeyConstraint fkc)
        {
            List<FileTranslator.ConstraintHint> constraintsList = this.ConstraintHelper.OtherConstraints;

            foreach (FileTranslator.ConstraintHint ch in constraintsList)
            {
                if (ch.ColumnNameNeedsConstraint == columnName && ch.IsReadOnly == true)
                {
                    fkc = null;
                    return ColumnProperty.READONLY;
                }

                fkc = ch as FileTranslator.ForeignKeyConstraint;
                if (fkc != null)
                {
                    if (fkc.ColumnNameNeedsConstraint == columnName)
                    {
                        return ColumnProperty.FOREIGN_KEY;
                    }

                    if (fkc.AssociatedColumns.ContainsKey(columnName))
                    {
                        return ColumnProperty.READONLY;
                    }
                }
            }

            fkc = null;
            return ColumnProperty.NORMAL;
        }

        public bool VerifyConstraint()
        {
            DataTableWrapper dtw = dataCache[0];
            if (dtw != null)
            {
                if (dtw.VerifyConstraint() == false)
                {
                    return false;
                }

                return true;
            }

            return false;
        }
    }

    public class FmtContainer : DataContainer
    {
        public FmtContainer()
            : base(new FmtConstraintHelper() )
        {
            Category = "文件格式清单";
            DescriptionColumn = "fmt_nm";
            this.GroupByHint = new string[2];
            GroupByHint[0] = "fmt_index";
            GroupByHint[1] = "fmt_nm";
        }
    }

    public class FldContainer : DataContainer
    {
        public FldContainer()
            : base(new FldConstraintHelper() )
        {
            Category = "文件字段元素清单";
            DescriptionColumn = "elem_desc";
            this.GroupByHint = new string[1];
            GroupByHint[0] = "elem_index";
        }
    }

    public class FldProcessContainer : DataContainer
    {
        public FldProcessContainer()
            : base(new FldProcessConstraintHelper() )
        {
            Category = "文件字段函数清单";
            DescriptionColumn = "func_name";
            this.GroupByHint = new string[1];
            GroupByHint[0] = "func_index";
        }
    }

    public class ElemBussIdContainer : DataContainer
    {
        public ElemBussIdContainer()
            : base(new ElemBussIdConstraintHelper())
        {
        }
    }

    public class SourFldTpContainer : DataContainer
    {
        public SourFldTpContainer()
            : base(new SourFldTpConstraintHelper())
        {
        }
    }
}
